/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * 个人信息
 */
import React, {Component} from "react";
import RefreshListView, {RefreshState} from "react-native-refresh-list-view";
import {
    StyleSheet,
    TouchableOpacity,
    Dimensions,
    Image,
    View,
    ToastAndroid,
    ScrollView,
    Button,
    Text,
    TextInput,
    FlatList
} from "react-native";
import api from "../../../api/api";
import Tips from "react-native-root-tips";
import AlteredLessonCell from "../../../widget/homework/AlteredLessonCell";
import {DefaultTabBar, ScrollableTabBar} from "react-native-scrollable-tab-view";
import method from "../../../api/method";
import {fetchGetRequest} from "../../../api/NetworkTool";
// import Toast from "react-native-easy-toast";
import EmptyCouponView from "../../../widget/CommonEmptyView";
type Props = {
    navigation: any,
}

type State = {
    typeIndex: number,
    data: Array<Object>,
}
const {height} = Dimensions.get('window')
export default class LessonListScene extends Component<State,Props> {

    static navigationOptions = ({
        navigation
    }: any) => ({
        headerTintColor: '#333333',
        headerStyle: {backgroundColor: 'white'},
        title: navigation.state.params.tag,
        headerLeft: <TouchableOpacity onPress={()=>{
            navigation.goBack(),
            navigation.state.params.callback('回调参数');
        }}>
            <Image style={{marginLeft:12}} source={require('../../../../res/img/gobal_back.png')}/>
        </TouchableOpacity>,
        headerRight: <Text/>
    })

    constructor(props: Object) {
        super(props)

        this.state = {
            typeIndex: 0,
            data: [],
            refreshState: RefreshState.Idle,
            course_id: this.props.navigation.state.params.course_id,
            category: this.props.navigation.state.params.category,
            tag: this.props.navigation.state.params.tag,
            homeworkItem: this.props.navigation.state.params.info,
            sub_title: '',
            btn_color: '',
            font_color: '',
        }
    }

    mapData = (res) => {
        let dataList = res.map((info) => {
            return {
                course_id: info.course_id,
                title: info.lesson_title,
                lession_id: info.lesson_id,
                teacher: info.teacher,
                homework_end_at:info.homework_end_at,
            }
        })
        return dataList
    }

    componentDidMount() {

        if (this.state.tag == '未批改') {
            this.setState({
                sub_title: '提交作业',
                btn_color: '#DB3843',
                font_color: '#FFFFFF',
            })
        } else if (this.state.tag == '已批改') {
            this.setState({
                sub_title: '批改详情',
                btn_color: '#DB3843',
                font_color: '#FFFFFF'
            })
        } else if (this.state.tag == '批改中') {
            this.setState({
                sub_title: '批改中',
                btn_color: '#EEEEEE',
                font_color: '#888888'
            })
        }

        this.requestNetData()
    }

    requestNetData = () => {

        this.setState({refreshState: RefreshState.HeaderRefreshing})

        fetchGetRequest(api.getLessonList, method.get, {category: this.state.category, course_id: this.state.course_id})
            .then(res => {
                console.log('=========', this.state.course_id)
                console.log('====$$$$$$$=====', res.lesson)
                let dataList = this.mapData(res.lesson)
                this.setState({
                    data: dataList,
                    refreshState: RefreshState.Idle,
                })

            })
            .catch(err => {
                this.setState({
                    refreshState: RefreshState.Failure,
                })
            })
    }

    onCellSelected = (info: Object) => {
        // this.props.navigation.navigate('alteredDetailScene', {lession_id: info.lession_id})
        if (this.state.tag == '未批改') {
            this.props.navigation.navigate('submitHomeworkScene', {
                info: this.state.homeworkItem, lession_id: info.lession_id, callback: (data) => {
                    this.requestNetData()
                }
            })
        } else if (this.state.tag == '已批改') {
            this.props.navigation.navigate('alteredDetailScene', {lession_id: info.lession_id})
        } else if (this.state.tag == '批改中') {
            Tips.showWarn('批改中，不可进行其他操作！', {mask: true})
        }

    }

    onsubmit = (info: Object) => {
        // this.props.navigation.navigate('alteredDetailScene', {lession_id: info.lession_id})
        if (this.state.tag == '未批改') {
            this.props.navigation.navigate('submitHomeworkScene', {
                info: this.state.homeworkItem, callback: (data) => {
                    this.requestNetData()
                }
            })
        } else if (this.state.tag == '已批改') {
            this.props.navigation.navigate('alteredDetailScene', {lession_id: info.lession_id})
        } else if (this.state.tag == '批改中') {
            Tips.showWarn('批改中，不可进行其他操作！', {mask: true})
        }

    }

    keyExtractor = (item: Object, index: number) => {
        return index + ''
    }
    render() {

        return (
            <View style={{flex:1, backgroundColor:'#FFFFFF'}}>
                <RefreshListView
                    ListHeaderComponent={() => (
                    !this.state.data.length?
                    <EmptyCouponView height={(height-200)} tip={'没有找到相关课节信息'} source={require('../../../../res/img/homework/no_homework.png')}/>
                     : null)
                    }
                    data={this.state.data}
                    renderItem={this.renderCell}
                    keyExtractor={this.keyExtractor}
                    refreshState={this.state.refreshState}
                    onHeaderRefresh={this.requestNetData}
                />
            </View>
        )
    }

    renderCell = (rowData: any) => {
        return (
            <AlteredLessonCell
                subtitle={this.state.sub_title}
                btn_color={this.state.btn_color}
                font_color={this.state.font_color}
                info={rowData.item}
                index={rowData.index}
                onPress={this.onCellSelected}
                onsubmit={this.onsubmit}
            />
        )
    }

}


